package com.xinqi.modules.sys.feedback.controller.web;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.common.core.enums.FeedbackTypeEnum;
import com.xinqi.common.log.asynclog.annotation.AsyncLog;
import com.xinqi.common.log.asynclog.enums.AsyncOperaType;
import com.xinqi.common.log.asynclog.enums.AsyncType;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.sys.feedback.dto.req.FeedbackCreateDTO;
import com.xinqi.modules.sys.feedback.dto.req.FeedbackDeleteDTO;
import com.xinqi.modules.sys.feedback.dto.req.FeedbackQueryDTO;
import com.xinqi.modules.sys.feedback.dto.req.FeedbackRewardedDTO;
import com.xinqi.modules.sys.feedback.dto.rsp.FeedbackResultDTO;
import com.xinqi.modules.sys.feedback.service.FeedbackService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * WEB -- 用户反馈
 *
 * @author: yyzhenn
 * @date: 2023/04/23
 */
@RestController
@RequestMapping("/web/feedback")
@RequiredArgsConstructor
public class FeedbackWebController {
    private final FeedbackService feedbackService;

    @Operation(summary = "反馈类型-枚举列表")
    @GetMapping("/type/list")
    public R<List<Map<String, String>>> typeList() {
        return R.data(FeedbackTypeEnum.getSelectList());
    }

    @Operation(summary = "用户反馈-分页")
    @GetMapping("/page")
    public R<PageInfo<FeedbackResultDTO>> page(@ParameterObject FeedbackQueryDTO dto) {
        dto.setFeedbackUserId(LoginHelper.getUserId());
        PageInfo<FeedbackResultDTO> page = feedbackService.page(dto, dto);
        return R.data(page);
    }

    @Operation(summary = "用户反馈-详情")
    @GetMapping("/info/{id}")
    public R<FeedbackResultDTO> info(@PathVariable("id") Long id) {
        return R.data(feedbackService.findById(id));
    }

    @Operation(summary = "用户反馈-校验是否是有奖反馈")
    @GetMapping("/check")
    public R<Boolean> check() {
        return R.data(feedbackService.checkRewarded(LoginHelper.getXqLoginUser().getId()));
    }

    @AsyncLog(title = "用户新增有奖反馈", asyncType = AsyncType.USER_FEEDBACK, operatorType = AsyncOperaType.USER_FEEDBACK)
    @Operation(summary = "用户反馈-新增有奖反馈")
    @PostMapping("/rewarded")
    public R<Long> rewarded(@Validated @RequestBody FeedbackRewardedDTO dto) {
        dto.setOperationUserId(LoginHelper.getXqLoginUser().getId());
        return R.data(feedbackService.rewarded(dto));
    }

    @AsyncLog(title = "用户新增普通反馈", asyncType = AsyncType.USER_FEEDBACK, operatorType = AsyncOperaType.USER_FEEDBACK)
    @Operation(summary = "用户反馈-新增普通反馈")
    @PostMapping("/add")
    public R<Long> insert(@Validated @RequestBody FeedbackCreateDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        return R.data(feedbackService.insert(dto));
    }

    @AsyncLog(title = "用户删除反馈", asyncType = AsyncType.USER_FEEDBACK, operatorType = AsyncOperaType.USER_FEEDBACK)
    @Operation(summary = "用户反馈-删除")
    @PostMapping("/remove")
    public R<Boolean> delete(@Validated @RequestBody FeedbackDeleteDTO dto) {
        return R.data(feedbackService.deleteById(dto));
    }

}
